/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *detectCycle(struct ListNode *head) {
    struct ListNode* slow, *fast;
    slow = fast = head;
    while(1)
    {
        if(fast == NULL || fast->next == NULL) return NULL;
        slow = slow->next;
        fast = fast->next->next;
        if(fast == slow) break;
    }
    fast = head;
    while(fast != slow)
    {
        slow = slow->next;
        fast = fast->next;
    }
    return fast;
}